<!DOCTYPE HTML>
<html>
<head>
    @include('common.header')
    <title>添加资讯</title>
    <style type="text/css">
        #container{
            width: 100%;
        }
    </style>
</head>
<body>
<article class="page-container">
    <form method="post" action="{{url('Article/edit.html')}}" class="form form-horizontal" id="form-article" enctype="multipart/form-data">
        <input type="hidden" name="id" value="{{$info['id'] or null}}">
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>文章标题：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="{{$info['title'] or null}}" placeholder="" id="title" name="title">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>分类栏目：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <span class="select-box">
				<select name="cat_id" class="select">
                    <option value="" <?php if(!isset($info['cat_id'])): ?>selected="selected"<?php endif; ?>>---请选择分类---</option>
                    @foreach($cats as $cat)
					<option value="{{$cat['id']}}" <?php if(isset($info['cat_id']) && $info['cat_id']==$cat['id']): ?>selected="selected"<?php endif; ?>>{{str_repeat('&nbsp;',$cat['depth']*4)}}{{$cat['name']}}</option>
                    @endforeach
				</select>
				</span>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>关键词：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="{{$info['keywords'] or null}}" placeholder="" id="keywords" name="keywords">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2">排序值：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="number" class="input-text" value="{{$info['sort'] or null}}" placeholder="" id="articlesort" name="sort">
            </div>
        </div>
        {{--<div class="row cl">--}}
            {{--<label class="form-label col-xs-4 col-sm-2">文章作者：</label>--}}
            {{--<div class="formControls col-xs-8 col-sm-9">--}}
                {{--<input type="text" class="input-text" value="0" placeholder="" id="author" name="author">--}}
            {{--</div>--}}
        {{--</div>--}}
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2">缩略图：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="file" accept="image/*" name="upload_thumbnail" id="upload_thumbnail" value="">
                @if(isset($info['thumbnail']) && $info['thumbnail'])
                <div><img id="thumbnail" width="320" height="auto" src="{{$info['thumbnail']}}"></div>
                @endif
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>文章内容：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <!-- 加载编辑器的容器 -->
                <textarea id="container" name="content" type="text/plain">{{$info['content'] or null}}</textarea>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"></label>
            <div class="formControls col-xs-8 col-sm-9">
                <button onClick="return save_submit();" class="btn btn-primary radius" type="submit"><i class="Hui-iconfont">&#xe632;</i> 保存</button>
            </div>
        </div>
    </form>

</article>
    @include('common.footer')
    <!--请在下方写此页面业务相关的脚本-->
    <script type="text/javascript" src="{{asset('hui-admin/lib/My97DatePicker/4.8/WdatePicker.js')}}"></script>
    <script type="text/javascript" src="{{asset('hui-admin/lib/ueditor/1.4.3/ueditor.config.js')}}"></script>
    <script type="text/javascript" src="{{asset('hui-admin/lib/ueditor/1.4.3/ueditor.all.min.js')}}"> </script>
    <script type="text/javascript" src="{{asset('hui-admin/lib/ueditor/1.4.3/lang/zh-cn/zh-cn.js')}}"></script>

    <!--/请在上方写此页面业务相关的脚本-->

    <!-- 实例化编辑器 -->
    <script type="text/javascript">
        var ue = UE.getEditor('container',{
            initialFrameHeight: 350,
            initialFrameWidth: 1000
        });

        function save_submit(){
            _id = $('input[name=id]').val();
            _title = $('input[name=title]').val();
            _cat_id = $('select[name=cat_id]').val();
            _sort = $('input[name=sort]').val();
            _keywords = $('input[name=keywords]').val();
            _content = $('textarea[name=content]').val();
            _thumbnail = $('#thumbnail').attr('src');
            if(isNull(_title) || isNull(_cat_id) || isNull(_keywords) || isNull(_content)){
                layer.msg('打星栏目请勿留空');
                return false;
            }
            _json_data = {'id':_id,'title':_title,'cat_id':_cat_id,'keywords':_keywords,'sort':_sort,'content':_content,'thumbnail':_thumbnail};
            $.ajax({
                type:'post',
                url:'/Article/edit.html',
                data:_json_data,
                dataType:'json',
                success:function(data){
                    if(data.code == 1) {
                        var FrameIndex = parent.layer.getFrameIndex(window.name);
                        parent.refresh();
                        parent.layer.close(FrameIndex);
                    }else{
                        layer.msg(data.msg);
                    }
                }
            });
            return false
        }

        var uploading = false;
        $("#upload_thumbnail").on("change", function(){
            if(uploading){
                alert("文件正在上传中，请稍候");
                return false;
            }
            $.ajax({
                url: "/Article/uploadImg",
                type: 'POST',
                cache: false,
                data: new FormData($('#form-article')[0]),
                processData: false,
                contentType: false,
                dataType:"json",
                beforeSend: function(){
                    uploading = true;
                },
                success : function(data) {
                    if (data.code == 1) {
                        if($("#thumbnail").length > 0) {
                            //元素存在时执行的代码
                            $("#thumbnail").attr("src", data.img_url);
                        }else{
                            $("#upload_thumbnail").after("<div><img id='thumbnail' width='320' height='auto' src='"+data.img_url+"'></div>");
                        }
                    } else {
                        showError(data.msg);
                    }
                    uploading = false;
                }
            });
        });

    </script>
</body>
</html>